/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.context.MessageSource;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.mttang.framework.core.paginated.util.PagedFliper;
import com.mttang.framework.core.paginated.util.Sheet;
import com.mttang.framework.core.paginated.web.controller.AbstractController;
import com.mttang.framework.core.paginated.web.dto.PagedDataModel;
import com.mttang.framework.core.pojos.ServiceRequest;
import com.mttang.framework.core.pojos.ServiceResponse;
import com.mttang.sys.biz.action.UserAction;
import com.mttang.util.InfoAppKeyConstant;
import com.mttang.util.Md5;
import com.mttang.util.PrvValidation;

/**
 * 类UserActionController.java的实现描述：有关用户的操作TODO 类实现描述
 * 
 * @author
 */
public class UserActionController extends AbstractController {

	private UserAction userAction;

	public void setUserAction(UserAction userAction) {
		this.userAction = userAction;
	}

	/**
	 * 用户查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			// 参数
			ModelAndView prvError = PrvValidation.prv(request, "query");
			if (prvError != null) {
				return prvError;
			}
			Map<String, Object> condition = new HashMap<String, Object>();
			Map<String, Object> map = new HashMap<String, Object>();
			if (request.getParameter("name") != null
					&& !request.getParameter("name").equals("")) {
				String name = request.getParameter("name").trim();
				name = name.replace("%", "\\%");
				name = name.replace("_", "\\_");
				map.put("name", name);
				request.setAttribute("name", request.getParameter("name"));
			}
			if (request.getParameter("empno") != null
					&& !request.getParameter("empno").equals("")) {
				String empno = request.getParameter("empno").trim();
				empno = empno.replace("%", "\\%");
				empno = empno.replace("_", "\\_");

				map.put("empno", empno);
				request.setAttribute("empno", request.getParameter("empno"));
			}
			if (request.getParameter("roleId") != null
					&& !request.getParameter("roleId").equals("")) {
				map.put("roleId", request.getParameter("roleId"));
				request.setAttribute("roleId", request.getParameter("roleId"));
			}
			// 部门ID
			String depId = StringUtils.hasLength(request.getParameter("depId")) ? request
					.getParameter("depId").trim() : null;
			map.put("depId", depId);
			String action = StringUtils.hasLength(request
					.getParameter("action")) ? request.getParameter("action")
					.trim() : null;

			String message = request.getParameter("message");
			if ("1".equals(message)) {
				message = msg.getMessage("sys.ok", null, Locale.CHINA);
			} else if ("2".equals(message)) {
				message = msg.getMessage("sys.empno.in", null, Locale.CHINA);
			} else if ("3".equals(message)) {
				message = msg
						.getMessage("sys.useremail.ok", null, Locale.CHINA);
			} else if ("4".equals(message)) {
				message = msg.getMessage("sys.useremail.error", null,
						Locale.CHINA);
			}
			String prv = request.getParameter("prv");
			map.put("prv", prv);
			condition.put(InfoAppKeyConstant.K_parameter, map);
			condition.put(InfoAppKeyConstant.K_pageModel, getFliper(request));
			Map<Object, Object> model = userAction.query(condition);
			PagedFliper page = (PagedFliper) model
					.get(InfoAppKeyConstant.K_pageModel);
			List<Object> data = (List<Object>) model
					.get(InfoAppKeyConstant.K_list);
			Sheet sheet = new Sheet(page.getRecordCount(), data);
			request.setAttribute("rolelist", model.get("rolelist"));
			request.setAttribute("message", message);
			if (action != null) {
				request.setAttribute("depId", depId);
				request.setAttribute("action", action);
				return new ModelAndView("user/sel_user", "bean",
						new PagedDataModel(sheet, page, 0));
			} else {
				return new ModelAndView("user/userquery", "bean",
						new PagedDataModel(sheet, page, 0));
			}
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "error_view",
					"Oops, error occur...");
		}
	}

	/**
	 * 为添加准备
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			Map<Object, Object> model = getGameList();
			return new ModelAndView("user/useradd", "success_view", model);
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "error_view",
					"Oops, error occur...");
		}
	}

	/**
	 * 用户添加
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView userAdd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			String empno = request.getParameter("empno");
			String passWord = new Md5().getHexMD5Str(empno);
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String[] roleId = request.getParameterValues("roleId");
			String depId = request.getParameter("depId");

			if (empno == null || "".equals(empno) || empno.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.empno.error", null, Locale.CHINA));
			}
			if (name == null || "".equals(name) || name.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.name.error", null, Locale.CHINA));
			}
			if (phone.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.phone.error", null, Locale.CHINA));
			}
			if (email.length() > 64) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.email.error", null, Locale.CHINA));
			}
			if (depId == null || "".equals(depId)) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.depId.error", null, Locale.CHINA));
			}
			if (roleId == null) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.roleId.error", null, Locale.CHINA));
			}
			String str = "";
			String strname = "";
			for (int i = 0; i < roleId.length; i++) {
				String[] ss = roleId[i].split(",");
				str = str + "," + ss[0];
				if (i == 0) {
					strname = ss[1];
				} else {
					strname = strname + "," + ss[1];
				}
			}
			str = str + ",";
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("empno", empno);
			condition.put("passWord", passWord);
			condition.put("name", name);
			condition.put("phone", phone);
			condition.put("email", email);
			condition.put("roleId", str);
			condition.put("roleName", strname);
			condition.put("depId", Integer.parseInt(depId));

			condition
					.put("subject", msg.getMessage("sys.useremail.subject",
							null, Locale.CHINA));
			condition.put("content",
					msg.getMessage("sys.useremail.content", null, Locale.CHINA)
							+ empno);

			// 日志
			Map<Object, Object> logMap = this.getLogParaInfo(request);
			logMap.put("ITEM", empno);// 操作数据项：这里是游戏名称
			logMap.put("CLASS", "2");// 操作类型
			logMap.put("NEWMAP", condition);
			condition.put("LOGMAP", logMap);
			Map<Object, Object> map = userAction.insert(condition);
			if (map != null && map.size() > 0) {
				if (map.get("userid") != null
						&& map.get("userid").toString().length() > 0) {
					return new ModelAndView(new RedirectView(
							"user.do?method=query&prv=query"), "message", "2");
				}
				if (map.get("status") != null
						&& map.get("status").toString().length() > 0) {
					String b = String.valueOf(map.get("status"));
					if ("true".equals(b)) {
						// message = msg.getMessage("sys.forgetpass.ok", null,
						// Locale.CHINA);// 密码发送到您的邮箱！！

						return new ModelAndView(new RedirectView(
								"user.do?method=query&prv=query"), "message",
								"3");
					} else {
						// message = msg.getMessage("sys.forgetpass.error",
						// null, Locale.CHINA);// 发送邮箱失败！！

						return new ModelAndView(new RedirectView(
								"user.do?method=query&prv=query"), "message",
								"4");
					}
				}
			}
			Map<Object, Object> model = new HashMap<Object, Object>();
			model.putAll(getGameList());
			// request.setAttribute("message", msg.getMessage("sys.ok", null,
			// Locale.CHINA));
			return new ModelAndView(new RedirectView(
					"user.do?method=query&prv=query"), "message", "1");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 用户修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");

		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			String empno = request.getParameter("empno");
			// String passWord=new Md5().getHexMD5Str(empno);
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String[] roleId = request.getParameterValues("roleId");
			String depId = request.getParameter("depId");
			String id = request.getParameter("id");
			String empno1 = request.getParameter("empno1");

			if (empno == null || "".equals(empno) || empno.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.empno.error", null, Locale.CHINA));
			}
			if (name == null || "".equals(name) || name.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.name.error", null, Locale.CHINA));
			}
			if (phone.length() > 20) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.phone.error", null, Locale.CHINA));
			}
			if (email.length() > 64) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.email.error", null, Locale.CHINA));
			}
			if (depId == null || "".equals(depId)) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.depId.error", null, Locale.CHINA));
			}
			if (id == null || "".equals(id)) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.id.error", null, Locale.CHINA));
			}
			if (roleId == null) {
				return new ModelAndView("error", "bean", msg.getMessage(
						"sys.roleId.error", null, Locale.CHINA));
			}
			String str = "";
			String strname = "";
			for (int i = 0; i < roleId.length; i++) {
				String[] ss = roleId[i].split(",");
				str = str + "," + ss[0];
				if (i == 0) {
					strname = ss[1];
				} else {
					strname = strname + "," + ss[1];
				}
			}
			str = str + ",";
			// 组装输入参数
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("empno", empno);
			condition.put("empno1", empno1);
			// condition.put("passWord", passWord);
			condition.put("name", name);
			condition.put("phone", phone);
			condition.put("email", email);
			condition.put("roleId", str);
			condition.put("roleName", strname);
			condition.put("depId", Integer.parseInt(depId));
			condition.put("id", Integer.parseInt(id));
			// 日志
			Map<Object, Object> logMap = this.getLogParaInfo(request);
			logMap.put("ITEM", empno);// 操作数据项：这里是游戏名称
			logMap.put("CLASS", "1");// 操作类型
			logMap.put("NEWMAP", condition);
			condition.put("LOGMAP", logMap);
			Map<Object, Object> map = userAction.update(condition);
			if (map != null && map.get("userid") != null
					&& map.get("userid").toString().length() > 0) {
				return new ModelAndView(new RedirectView(
						"user.do?method=query&prv=query"), "message", "2");
			}
			Map<Object, Object> model = new HashMap<Object, Object>();
			model.putAll(getGameList());
			return new ModelAndView(new RedirectView(
					"user.do?method=query&prv=query"), "message", "1");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 根据ID查询用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView searchById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		if (id == null || "".equals(id)) {
			return new ModelAndView(new RedirectView("error"), "bean",
					msg.getMessage("sys.id.error", null, Locale.CHINA));
		}
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", Integer.parseInt(id));
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			Map<Object, Object> model = userAction.serarchById(condition);
			model.putAll(getGameList());
			String[] ss = String.valueOf(model.get("roleId")).split(",");
			List<Map<String, String>> list = (List<Map<String, String>>) model
					.get("rolelist");
			List<Map<String, String>> list1 = new ArrayList<Map<String, String>>();
			for (int i = 0; i < list.size(); i++) {
				Map<String, String> map = list.get(i);
				for (int j = 0; j < ss.length; j++) {
					if (String.valueOf(map.get("ROLE_ID")).equals(ss[j])) {
						map.put("checked", "checked");
						break;
					} else {
						map.put("checked", "");
					}
				}
				list1.add(map);
			}
			if (type.equals("1")) {
				return new ModelAndView("user/userupdate", "success_view",
						model);
			} else {
				return new ModelAndView("user/userlook", "success_view", model);
			}
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 用户删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");

		String id = request.getParameter("id");
		if (id == null || "".equals(id)) {
			return new ModelAndView("error", "bean", msg.getMessage(
					"sys.id.error", null, Locale.CHINA));
		}
		String userid = ((Map) request.getSession().getAttribute("user")).get(
				"userid").toString();
		if (userid.equals(id)) {
			return new ModelAndView("error", "bean", msg.getMessage(
					"sys.user.delete.error", null, Locale.CHINA));
		}
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", Integer.parseInt(id));
		// 日志
		Map<Object, Object> logMap = this.getLogParaInfo(request);
		logMap.put("ITEM", id);// 操作数据项：这里是游戏名称
		logMap.put("CLASS", "0");// 操作类型
		logMap.put("NEWMAP", condition);
		condition.put("LOGMAP", logMap);

		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			userAction.deleteUpdate(condition);
			Map<Object, Object> model = new HashMap<Object, Object>();
			model.putAll(getGameList());
			return new ModelAndView(new RedirectView(
					"user.do?method=query&prv=query"), "message", "1");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 用户密码重置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView passReset(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		String id = request.getParameter("id");
		String empno = request.getParameter("empno");
		if (id == null || "".equals(id)) {
			return new ModelAndView("error", "bean", msg.getMessage(
					"sys.id.error", null, Locale.CHINA));
		}
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", Integer.parseInt(id));
		condition.put("passWord", new Md5().getHexMD5Str(empno));
		// 日志
		Map<Object, Object> logMap = this.getLogParaInfo(request);
		logMap.put("ITEM", empno);// 操作数据项：这里是游戏名称
		logMap.put("CLASS", "1");// 操作类型
		logMap.put("NEWMAP", condition);
		condition.put("LOGMAP", logMap);
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			userAction.pass(condition);
			Map<Object, Object> model = new HashMap<Object, Object>();
			model.putAll(getGameList());
			return new ModelAndView(new RedirectView(
					"user.do?method=query&prv=query"), "message", "1");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 用户密码修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView pass(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");

		String id = request.getParameter("id");
		String passWord = request.getParameter("passWord");
		String passWord1 = request.getParameter("passWord1");
		String passWord2 = request.getParameter("passWord2");
		if (id == null || "".equals(id)) {
			return new ModelAndView("error", "bean", msg.getMessage(
					"sys.id.error", null, Locale.CHINA));
		}
		if (!passWord1.equals(passWord2)) {
			return new ModelAndView("user/userpassupdate", "message",
					msg.getMessage("sys.password1.error", null, Locale.CHINA));
		}
		HttpSession session = request.getSession();
		Map<String, String> user = (Map) session.getAttribute("user");
		String pass = user.get("passWord");
		if (!pass.equals(new Md5().getHexMD5Str(passWord))) {
			request.setAttribute("message",
					msg.getMessage("sys.password.error", null, Locale.CHINA));
			return new ModelAndView("user/userpassupdate");
		}
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", Integer.parseInt(id));
		condition.put("passWord", new Md5().getHexMD5Str(passWord1));
		// 日志
		Map<Object, Object> logMap = this.getLogParaInfo(request);
		logMap.put("ITEM", id);// 操作数据项：这里是游戏名称
		logMap.put("CLASS", "1");// 操作类型
		logMap.put("NEWMAP", condition);
	    condition.put("LOGMAP", logMap);
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			userAction.pass(condition);
			Map<Object, Object> model = userAction.serarchById(condition);
			request.getSession().removeAttribute("user");
			request.getSession().setAttribute("user", model);
			request.setAttribute("message",
					msg.getMessage("sys.password.ok", null, Locale.CHINA));
			return new ModelAndView("user/userpassupdate");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 用户信息修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView info(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 找到本controller在APP中对应的Action.
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		String id = request.getParameter("id");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		if (id == null || "".equals(id)) {
			return new ModelAndView("error", "bean", msg.getMessage(
					"sys.id.error", null, Locale.CHINA));
		}
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", Integer.parseInt(id));
		condition.put("phone", phone);
		condition.put("email", email);
		// 日志
		Map<Object, Object> logMap = this.getLogParaInfo(request);
		logMap.put("ITEM", id);// 操作数据项：这里是游戏名称
		logMap.put("CLASS", "1");// 操作类型
		logMap.put("NEWMAP", condition);
		condition.put("LOGMAP", logMap);
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
		    userAction.update(condition);
			Map<Object, Object> model = userAction.serarchById(condition);
			request.getSession().removeAttribute("user");
			request.getSession().setAttribute("user", model);
			request.setAttribute("message",
					msg.getMessage("sys.info.ok", null, Locale.CHINA));
			return new ModelAndView("user/userinfoupdate");
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
	}

	/**
	 * 跳转到用户信息修改页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView infoUpdate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return new ModelAndView("user/userinfoupdate", "success_view", null);
	}

	/**
	 * 跳转到用户密码修改页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView passUpdate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return new ModelAndView("user/userpassupdate", "success_view", null);
	}

	/**
	 * 工号是否唯一
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView empno(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MessageSource msg = (MessageSource) this.getApplicationContext()
				.getBean("messageSource");
		String empno = request.getParameter("empno");
		// 组装输入参数
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("empno", empno);
	
		try {
			if (logger.isInfoEnabled()) {
				logger.info("调用远程接口: userAction");
			}
			Map<Object, Object> model = userAction.empno(condition);
			StringBuffer str = new StringBuffer();
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = null;
			out = response.getWriter();
			if (model.get("userid") != null
					&& model.get("userid").toString().length() > 0) {
				str.append(msg.getMessage("sys.empno.in", null, Locale.CHINA));
			} else {
				str.append("");
			}
			out.print(str.toString());
			out.close();
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "bean", "Oops, error occur...");
		}
		return null;
	}

	/**
	 * 获取部门和角色的LIST
	 * 
	 * @param serviceRequest
	 * @return
	 */
	private Map<Object, Object> getGameList() {

		Map<Object, Object> condition = new HashMap<Object, Object>();

		Map<Object, Object> model = userAction.list(condition);

		return model;
	}

	/**
	 * 日志IP和工号
	 * 
	 * @param request
	 * @return
	 */
	private Map<Object, Object> getLogParaInfo(HttpServletRequest request) {
		// 写日志用到的信息
		String ip = request.getHeader("x-forwarded-for");
		if ("unknown".equalsIgnoreCase(ip) || ip == null) {
			ip = request.getRemoteAddr();
		}

		Map<Object, Object> userMap = (Map<Object, Object>) request
				.getSession().getAttribute("user");
		if (userMap == null) {
			userMap = new HashMap<Object, Object>();
		}
		Map<Object, Object> returnMap = new HashMap<Object, Object>();
		returnMap.put("OPTIONOR", userMap.get("empno"));
		returnMap.put("IP", ip);

		return returnMap;
	}

	// ajax 按部门读取用户信息，不分页
	public ModelAndView loadUserListByDeptId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String deptId = request.getParameter("deptId");

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("deptId", deptId);
	

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		try {
			Map model = userAction.fingUserList(condition);
			
			List<Map<String, Object>> list = (List<Map<String, Object>>) model
					.get("result");
			JSONArray jsonArray = new JSONArray();
			for (Map<String, Object> map : list) {
				jsonArray.put(new JSONObject(map));
			}
			logger.debug(jsonArray.toString());
			out.print(jsonArray.toString());
		} catch (Exception e) {
			logger.error(e);
			return new ModelAndView("error", "error_view",
					"Oops, error occur...");
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}

		return null;
	}
}
